(function($, doc) {
	$.init();
	$.plusReady(function() {
		var user = app.getState();
		var uid = user.user_id;
		var self = plus.webview.currentWebview();
		var addrs = self.address;
		var name = self.name;
		var outaddr = self.outaddr;
		var pricelist = JSON.parse(localStorage.getItem('coinprice'));
		var coinlist = JSON.parse(localStorage.getItem('mycoinlist' + uid));
		var price = pricelist[name];
		var coindata = ethscan.getCoindata(addrs);
		var ctype = coindata['ctype'];
		//var coindata = coinlist[addrs];
		//var ctype = coindata['ctype'] ? coindata['ctype'] : 'ethcontact';
		//console.log(JSON.stringify(coindata));
		//console.log(price);
		doc.getElementById('coinname').innerHTML = name;
		var myWallet = ethscan.getWallet(ctype);
		var address = myWallet.address;
		if(!address) {
			$.alert("请创建或导入钱包");
			$.openWindow({
				url: "mgrwallet.html",
				id: "mgywallet"
			});
		}
		if(ctype == 'bitcontract') {

		}

		//ethscan.clear();
		/*
		if(ctype == 'ethcontact'){
			//console.log(addrs);
			coindata['abi'] = ethscan.getabi(addrs);
			//console.log(coindata['abi']);
		}*/
		doc.getElementById('myaddr').innerHTML = address;
		
		
		new Clipboard('#copyaddr', {
			text: function() {
				return address;
			}
		}).on('success', function(e) {
			mui.toast("已复制钱包地址");
		});
		
		
		ethscan.getAmount(coindata, function(money, address) {
			doc.getElementById('allmoney').innerHTML = money;
			var allprice = price * money;
			doc.getElementById('allprice').innerHTML = '≈$' + allprice;
		});
		

		var qrcode = new QRCode('qr', {
			text: 'coin@' + address + '@' + coindata.address + '@' + name,
			width: 250,
			height: 250,
			colorDark: '#000000',
			colorLight: '#ffffff',
			correctLevel: QRCode.CorrectLevel.H
		});
		
		doc.getElementById('showqrscan').addEventListener('tap', function() {
			plus.webview.close('weep', 'none');
			plus.webview.open('../../pages/find/weep.html', 'weep');

		});
		
		//var outaddr ='234324';
		if(outaddr) {
			doc.getElementById('pay_addr').value = outaddr;
			doc.querySelector('#qrcode').classList.remove('mui-active');
			doc.querySelector('.mui-active').classList.remove('mui-active');
			doc.querySelector('.payout').classList.add('mui-active');
			doc.querySelector('#paymoney').classList.add('mui-active');
		}

		doc.getElementById('paysub').addEventListener('tap', function() {
			var pay_addr = doc.getElementById('pay_addr').value;
			var pay_money = doc.getElementById("pay_money").value;
			if(!pay_addr) {
				$.toast("付款地址不能为空");
				return;
			}
			if(pay_addr == address) {
				$.alert('不能自己给自己付款！');
				return;
			}

			if(!pay_money) {
				$.toast("付款金额不能为空");
				return;
			}
			if(isNaN(pay_money)) {
				$.toast("付款金额必须为数字");
				return;
			}
			if(doc.getElementById('allmoney').innerHTML * 1 < pay_money) {
				$.toast("额度不足");
				return;
			}
			ethscan.payToAddrByPwd(pay_addr, ctype, coindata, pay_money, function(res) {
				if(res) {
					$.alert("付款成功！");
				} else {
					$.alert("付款失败");
				}
			}, function(err) {
				console.log(err);
				$.alert("付款失败");
			});
		});
		
		
		var renderpaylog = function(page, size, callback) {
			var url;
			
			console.log(ctype);
			//if(!page) page = 1;
			if(page == 0) {
				page = 1;
			}
			if(ctype == 'ethcontact') {
				url = apiurl + 'eth/gettxlist?addr=' + address + '&caddr=' + coindata['address'] + "&p=" + page;
			} else if(ctype == 'eth') {
				url = apiurl + 'eth/gettxslist?addr=' + address + "&p=" + page;
			} else if(ctype == 'btc') {
				url = apiurl + 'eth/getbtctxlist?addr=' + address + "&p=" + page;
			} else {
				return;
			}
			console.log(url);
			
			$.getJSON(url, function(data) {
				console.log(data.length);
				if (data && data.length > 0) {
					callback(template("paylog-template", {
						data: data
					}), data.length);
				} else {
					callback('', 0);
				}
			});
		}
		
		new Clipboard('.copy-hash', {
			text: function(dom) {
				return dom.parentNode.id;
			}
		}).on('success', function (e) {
			mui.toast("已复制Hash");
		});
		

		$("#paylogarea").on('tap', '.mui-table', function() {
			if(ctype == 'eth' || ctype == 'ethcontact') {
				plus.runtime.openURL('https://etherscan.io/tx/' + this.parentNode.id);
			}
			if(ctype == 'btc') {
				plus.runtime.openURL('https://www.blocktrail.com/BTC/tx/' + this.parentNode.id);
			}
		});
		

		gatc.lazyload('#paylogarea', {
			loadData: renderpaylog,
			pageSize: ctype === 'ethcontact' ? 20 : 50,
			container: '.mui-content'
		});
		
	});

}(mui, document));